package com.he.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.he.entity.ResultData;
import com.he.entity.Room;
import com.he.entity.RoomPrice;
import com.he.service.IRoomPriceService;
import com.he.service.IRoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/room")
@Slf4j
public class RoomController {
    @Autowired
    private IRoomService roomService;

    @Autowired
    private IRoomPriceService roomPriceService;

    /**
     * 根据hid查询当前酒店的房间列表
     * @param hid
     * @return
     */
    @RequestMapping("/getRoomByHid/{hid}")
    ResultData<List<Room>> getRoomByHid(@PathVariable Integer hid){
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hid",hid);
        List<Room> list = roomService.list(queryWrapper);
        //log.info("查出的酒店:"+list);
        return new ResultData<List<Room>>().setData(list);
    }

    @RequestMapping("/getHotelNameByHid/{hid}")
    String getHotelNameByHid(@PathVariable Integer hid){
        return roomService.getHotelNameByHid(hid);
    }

    @RequestMapping("/saveRoom")
    ResultData<Boolean> saveRoom(@RequestBody Room room){
        //log.info("添加的房间类型"+room);
        boolean flag = roomService.save(room);
        return new ResultData<Boolean>().setData(flag);
    }

    @RequestMapping("/updateBNum/{hid}/{num}")
    public void updateBNum(@PathVariable("hid") Integer bid,@PathVariable("num") Integer num){
        log.info("被修改的房间"+bid+",数量"+num);
        roomService.updateBNum(bid,num);
    }


    /**
     * 获得房间价格
     * @return
     */
    @RequestMapping("/getPrice")
    public ResultData<List<RoomPrice>> getRoomPrice(@RequestParam Integer rid){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("rid", rid);
        List<RoomPrice> list = roomPriceService.list(queryWrapper);
        return new ResultData<List<RoomPrice>>().setData(list);
    }

    /**
     * 修改房间价格
     * @return
     */
    @RequestMapping("/updateprice")
    public ResultData<Boolean> updatePrice(@RequestBody RoomPrice roomPrice){
        boolean flag = roomPriceService.updateById(roomPrice);
        return new ResultData<Boolean>().setData(flag);
    }


    /**
     * 根据客房id查询对应的酒店id
     * @param rid
     * @return
     */
    @RequestMapping("/getHotelId/{rid}")
    public ResultData<Integer> getHotelIdByRoomId(@PathVariable Integer rid){
        Room room = roomService.getById(rid);
        return new ResultData<Integer>().setData(room.getHid());
    }

    /**
     * 根据客房id获得客房信息
     * @return
     */
    @RequestMapping("/getRoom/{rid}")
    public ResultData<Room> getRoomById(@PathVariable Integer rid){
        Room room = roomService.getById(rid);
        return new ResultData<Room>().setData(room);
    }

}
